package com.myblog.controller;

import com.myblog.constant.CodeType;
import com.myblog.model.User;
import com.myblog.service.LeaveService;
import com.myblog.service.UserService;
import com.myblog.utils.DataMap;
import com.myblog.utils.JsonResult;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Select;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.security.Principal;

@Slf4j
@RestController
public class LeaveController {
    @Autowired
    LeaveService leaveService;

    @Autowired
    private UserService userService;

    @GetMapping("/newLeaveWord")
    public String getNewLeaveWord(@RequestParam(value = "rows") Integer rows, @RequestParam(value = "pageNum") Integer pageNum){
        try {
            //调取service层的方法
            DataMap data = leaveService.getNewLeave(rows,pageNum);
            return JsonResult.build(data).toJSON();
        } catch (Exception e) {
            log.error("LeaveController getNewLeaveWord Exception" ,e);
        }
        return JsonResult.fail().toJSON();
    }

    /**
     * 根据页面获取留言
     * @param pageName
     * @return
     */
    @GetMapping("/getPageLeaveMessage")
    public String getPageLeaveMessage(@RequestParam(value = "pageName")String pageName,HttpServletRequest request){
        try{
            //获取点赞人
            Principal userPrincipal = request.getUserPrincipal();
            String userName = null;
            //判断用户是否已经点赞了评论
            if(userPrincipal != null){
                userName = userPrincipal.getName();
            }
            //获取留言信息
            DataMap data = leaveService.getLeaveByPageName(pageName,userName);
            return JsonResult.build(data).toJSON();
        }catch (Exception e){
            log.error("LeaveController getPageLeaveMessage Exception",e);
        }
        return JsonResult.fail(CodeType.SERVER_EXCEPTION).toJSON();
    }

    /**
     * 用户个人界面获取留言
     * @param rows
     * @param pageNum
     * @param request
     * @return
     */
    @PostMapping("/getUserLeaveWord")
    public String getUserLeaveWord(@RequestParam(value = "rows") Integer rows, @RequestParam(value = "pageNum") Integer pageNum,
                                 HttpServletRequest request){
        try {
            Principal userPrincipal = request.getUserPrincipal();
            if(userPrincipal == null){
                return JsonResult.fail(CodeType.USER_NOT_LOGIN).toJSON();
            }
            User user = userService.searchUserByName(userPrincipal.getName());
            Integer userId = user.getId();
            DataMap data = leaveService.getUserLeaveWord(rows,pageNum,userId);
            return JsonResult.build(data).toJSON();
        } catch (Exception e) {
            log.error("LeaveController LeaveController Exception",e);
        }
        return JsonResult.fail().toJSON();
    }

    /**
     * 发表留言
     * @param leaveMessageContent
     * @param pageName
     * @param request
     * @return
     */
    @PostMapping("/publishLeaveMessage")
    public String publishLeaveMessage(@RequestParam(value = "leaveMessageContent")String leaveMessageContent,
                                      @RequestParam(value = "pageName")String pageName,
                                      HttpServletRequest request){
        try{
            Principal userPrincipal = request.getUserPrincipal();
            if(userPrincipal == null){
                return JsonResult.fail(CodeType.USER_NOT_LOGIN).toJSON();
            }
            User user = userService.searchUserByName(userPrincipal.getName());
            int answerId = user.getId();
            DataMap data = leaveService.publishLeaveMessage(leaveMessageContent,pageName,answerId);

            return JsonResult.build(data).toJSON();
        }catch (Exception e){
            log.error("CategoryController findAllCategoriesName Exception",e);
        }
        return JsonResult.fail(CodeType.SERVER_EXCEPTION).toJSON();
    }

    /**
     * 对留言回复
     * @return
     */
    @PostMapping("/publishLeaveMessageReply")
    public String publishLeaveMessageReply(@RequestParam(value = "leaveMessageContent")String leaveMessageContent,
                                           @RequestParam(value = "pageName")String pageName,
                                           @RequestParam(value = "parentId")String pIdString,
                                           @RequestParam(value = "respondent")String respondentName,
                                           HttpServletRequest request){
        try{
            Principal userPrincipal = request.getUserPrincipal();
            if(userPrincipal == null){
                return JsonResult.fail(CodeType.USER_NOT_LOGIN).toJSON();
            }
            User user = userService.searchUserByName(userPrincipal.getName());
            int answerId = user.getId();
            DataMap data = leaveService.ReplyLeaveMessage(leaveMessageContent,pageName,answerId,pIdString,respondentName);

            return JsonResult.build(data).toJSON();
        }catch (Exception e){
            log.error("CategoryController findAllCategoriesName Exception",e);
        }
        return JsonResult.fail(CodeType.SERVER_EXCEPTION).toJSON();
    }
}
